// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   FileUpload.java

package com.iphaeton.common.util.upload;

import com.iphaeton.common.util.DateUtil;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

public class FileUpload
{

    public FileUpload()
    {
    }

    public String uploadFile(ActionForm form, String filepath)
        throws FileNotFoundException, IOException
    {
        String FileName = "";
        String returnFile_ = "";
        if(form == null || form.getMultipartRequestHandler() == null)
            return "";
        Hashtable files = form.getMultipartRequestHandler().getFileElements();
        FormFile f[] = (FormFile[])null;
        if(files != null && files.size() > 0)
        {
            f = new FormFile[files.size()];
            int i = 0;
            String fname = "";
            for(Enumeration enumFiles = files.keys(); enumFiles.hasMoreElements();)
            {
                fname = (String)enumFiles.nextElement();
                f[i] = (FormFile)files.get(fname);
                i++;
            }

            int realFileCount = 0;
            for(int a = 0; a < f.length; a++)
            {
                FileName = f[a].getFileName();
                if(f[a] != null && f[a].getFileSize() > 0 && FileName != null && !FileName.equals(""))
                {
                    InputStream stream = f[a].getInputStream();
                    returnFile_ = DateUtil.getDateString().trim() + FileName.substring(FileName.lastIndexOf("."), FileName.length()).trim().toLowerCase();
                    File f1_ = new File(filepath);
                    if(!f1_.exists())
                        f1_.mkdirs();
                    OutputStream bos = new FileOutputStream(filepath + "/" + returnFile_);
                    int bytesRead = 0;
                    byte buffer[] = new byte[8192];
                    while((bytesRead = stream.read(buffer, 0, 8192)) != -1) 
                        bos.write(buffer, 0, bytesRead);
                    bos.close();
                    stream.close();
                    realFileCount++;
                }
            }

        }
        return returnFile_;
    }
}
